package org.jradweb.tools.generator.xml;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.maven.plugin.logging.Log;
import org.jradweb.tools.generator.freemarker.Value;
import org.jradweb.tools.generator.utils.LogImpl;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 * for add hibernate mapping
 *
 * Created On 2008-3-26
 * @author <a href="mailto:wagzhi@gmail.com">Paul Wang</a>
 * @version $Id: HibernateConfigOperator.java 148 2008-10-10 03:07:23Z wagzhi $
 */
public class HibernateConfigOperator extends XmlOperator {
	
	public HibernateConfigOperator(File rootPath, String groupId,
			String artifactId, Log log) throws IOException, ParserConfigurationException, SAXException {
		super(rootPath, groupId, artifactId, log);
		this.setXmlFile(new File(rootPath.getAbsoluteFile()+"/src/main/resources/hibernate.cfg.xml"));
		if(!this.getXmlFile().exists())
		{
			this.getLog().error("hibernate config file not exits, please create id manually: "+this.getXmlFile().getAbsolutePath());
		}
		this.document=this.initDocument(this.getXmlFile());
	}
	public void addClassMapping(String modelName) throws Exception{
		Value model=new Value(modelName);
		Element config = document.getDocumentElement();
		Node sessionFactory = this.getNodeByName("session-factory");
		if(sessionFactory==null){
			getLog().error("sessionFactory not exists in hibernaite config, add mapping for "+modelName+" failed");
			return;
		}
		//check if this mapping already exists
		String mappingClass=this.groupId+"."+this.artifactId+".model."+model;
		
		NodeList nodeList=sessionFactory.getChildNodes();
		for(int i=0;i<nodeList.getLength();i++){
			Node node=nodeList.item(i);
			if(node.getNodeName().equals("mapping")){
				NamedNodeMap nnm = node.getAttributes();
				if(nnm!=null){
					for(int j=0;j<nnm.getLength();j++){
						Node attr=nnm.item(j);
						if(attr.getNodeName().equals("class")&&attr.getNodeValue().equals(mappingClass)){
							getLog().info("hibernate mapping class \""+mappingClass+"\" already exists!");
							return;
						}
					}
				}
			}
		}
		
		
		
		/*------------------------------------------------*/
		/*    add new mapping(id and class)		  */
		/*------------------------------------------------*/
		Element mapping=this.document.createElement("mapping");
		mapping.setAttribute("class", mappingClass);
		sessionFactory.appendChild(mapping);
		
		/*------------------------------------------------*/
		/*    save docment								  */
		/*------------------------------------------------*/
		this.saveDocument(XML_TYPE_HIBERNATE);
		this.getLog().info("hibernate mapping \""+mappingClass+"\" added!");
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HibernateConfigOperator opr;
		try {
			File projectRootPath = new File("D:/workspace2/jradtest");
			opr = new HibernateConfigOperator(projectRootPath, "org.jradweb","jradtest",new LogImpl(HibernateConfigOperator.class));
			opr.addClassMapping("customer");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 

	}

}
